package com.ebt.m.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.customer.view.ConfirmDialog;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.event.ProposalListTabCheckedEvent;
import com.ebt.m.presenter.ProposalListPresenter;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.proposal_v2.utils.Utils;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.concurrent.TimeUnit;

/**
 * Created by andy on 17-5-31.
 */

public class ProposalListView extends BaseListView<ProposalListPresenter> {
    private int position;
    TextView textView;
    ConfirmDialog mDeleteConfirmDialog;
    ImageView emptyImageView;

    public ProposalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProposalListView(Context context) {
        super(context);
    }

    public ProposalListView(Context context, int position) {
        super(context);
        this.position = position;
        EventBus.getDefault().register(this);
    }

    public ProposalListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            try {
                ProposalListItem listItem = (ProposalListItem) args[0];

                switch (view.getId()) {
                    case R.id.btn_delete:
                        mDeleteConfirmDialog = new ConfirmDialog(getContext());
                        mDeleteConfirmDialog.setMessage("确定要删除吗？");
                        mDeleteConfirmDialog.setPositiveButton("删除", new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mDeleteConfirmDialog.dismiss();
                                mPresenter.deleteProposal(listItem.pickNum);
                            }
                        });
                        mDeleteConfirmDialog.show();

                        break;
                    case R.id.content:
                        ProposalUtils.gotoItemFromProposalList(getContext(), listItem);
                        break;
                }
            } catch (Exception e) {
                Logger.e(e);
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    @Override
    protected ISectionFactory createViewFactory() {
        return new ISectionFactory() {
            @Override
            public IView createView(int viewType) {
                ProposalAllItemView itemView = new ProposalAllItemView(getContext());
                itemView.setOnClickViewListener(onClickListner);
                return itemView;
            }
        };
    }

    @Override
    protected ProposalListPresenter createPresenter() {
        return new ProposalListPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {
        mPresenter.loadNext(position);
    }

    @Override
    protected boolean createFootView() {
        return true;
    }

    @Override
    public void update(Object... args) {
        super.update(args);
        position = (int) args[0];
        textView.setText(getEmptyText(position));
        emptyImageView.setBackgroundResource(getEmptyImg(position));
        mPresenter.loadNew(args);
        if (!NetUtils.isNetworkConnected(getContext())) {
            ToastUtil.show(getContext(), getResources().getString(R.string.network_fail));
        }
    }

    private String getEmptyText(int position) {
        String emptyText = "";
        switch (position) {
            case 0:
                emptyText = "暂无计划书";
                break;
            case 1:
                emptyText = "暂无草稿哦";
                break;
            case 2:
                emptyText = "暂无未发送的计划书";
                break;
            case 3:
                emptyText = "暂无已发送的计划书";
                break;
            case 4:
                emptyText = "暂无被查看的计划书";
                break;
        }
        return emptyText;
    }

    private int getEmptyImg(int position) {
        int emptyImg = R.drawable.ic_proposal_empty;
        switch (position) {
            case 0:
                emptyImg = R.drawable.ic_proposal_empty;
                break;
            case 1:
                emptyImg = R.drawable.ic_proposal_draft_empty;
                break;
            case 2:
            case 3:
            case 4:
                emptyImg = R.drawable.ic_proposal_empty;
                break;
        }
        return emptyImg;
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.proposal_list_empty, null);
        textView = (TextView) emptyContent.findViewById(R.id.tv_empty);
        textView.setText("");
        emptyImageView = emptyContent.findViewById(R.id.iv_empty);
        Button btn_new = (Button) emptyContent.findViewById(R.id.btn_empty);
        RxView.clicks(btn_new)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(aVoid -> {
                    ProposalUtils.gotoMakeNewProposal(getContext());
                });
        return emptyContent;
    }

    @Subscribe
    public void onTabChangedEvent(ProposalListTabCheckedEvent event) {
        if (event.position == position) {
            Utils.print("DraftUpdate", "++++建议书列表开始更新++++");
            update(event.position);
        }
    }

}
